all : default

TARGET    = coffee.exe
CC        = ccache gcc
CXX       = ccache g++
RM        = rm -f
NDEBUG    = 0

ifneq ($(OS), Windows_NT)
 ifneq ($(NDEBUG), 1)
 CFLAGS    = -g -ggdb -Wall -fomit-frame-pointer -fno-strict-aliasing -frandom-seed=$* \
             -DLUA_COMPAT_ALL -DPNG_THREAD_UNSAFE_OK -DPNG_NO_MMX_CODE -DPNG_NO_MNG_FEATURES \
             -DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_NEED_FOR_SPEED_GPU -D__STDC_CONSTANTS \
             -D__USE_LONG_HEADER_PATH \

  ifdef NODEP
  CFLAGS  += -DNO_DEPEND
  endif
 else
 CFLAGS    = -02 -Wall -fomit-frame-pointer -fno-strict-aliasing -frandom-seed=$* \
             -DLUA_COMPAT_ALL -DPNG_THREAD_UNSAFE_OK -DPNG_NO_MMX_CODE -DPNG_NO_MNG_FEATURES \
             -DLUA_USE_POSIX -DLUA_USE_DLOPEN -D_NEED_FOR_SPEED_GPU -D__STDC_CONSTANTS \
             -D__USE_LONG_HEADER_PATH \

  ifdef NODEP
  CFLAGS  += -DNO_DEPEND
  endif
 endif
else
 ifneq ($(NDEBUG), 1)
 CFLAGS    = -g -ggdb -Wall -fomit-frame-pointer -fno-strict-aliasing -frandom-seed=$* \
             -DLUA_COMPAT_ALL -DPNG_THREAD_UNSAFE_OK -DPNG_NO_MMX_CODE -DPNG_NO_MNG_FEATURES \
             -D__STDC_CONSTANTS -D__USE_LONG_HEADER_PATH \

  ifdef NODEP
  CFLAGS  += -DNO_DEPEND
  endif
 else
 CFLAGS    = -02 -Wall -fomit-frame-pointer -fno-strict-aliasing -frandom-seed=$* \
             -DLUA_COMPAT_ALL -DPNG_THREAD_UNSAFE_OK -DPNG_NO_MMX_CODE -DPNG_NO_MNG_FEATURES \
             -D__STDC_CONSTANTS -D__USE_LONG_HEADER_PATH \

  ifdef NODEP
  CFLAGS  += -DNO_DEPEND
  endif
 endif
endif

CXXFLAGS  = $(CFLAGS)

INCPATH   = -I/usr/include \
            -I/usr/local/include \
            -I./CoffeeMate/glm \
            -I./CoffeeMate/zlib \
            -I./CoffeeMate/libpng \
            -I./CoffeeMate/sqlite \
            -I./CoffeeMate/irrxml \
            -I./CoffeeMate/lua \

LIBPATH   = -L/usr/lib \
            -L/usr/local/lib \

ifeq ($(OS), Windows_NT)
LDLIBS    = -lmingw32 \
            -lm \
            -lpthread \
            -lSDLmain \
            -lSDL \
            -lopengl32 \
            -lglu32 \
            -lgdi32 \
            -luser32 \
            -lstdc++ \
            -mwindows \

else
LDLIBS    = -lstdc++ \
            -lSDL \
            -lGL \
            -lGLU \
            -lm \
            -lpthread \
            -lX11 \
            -ldl \

endif

SRCS      = $(wildcard ./*.cpp) \
            $(wildcard ./CoffeeMate/zlib/*.c) \
            $(wildcard ./CoffeeMate/libpng/*.c) \
            $(wildcard ./CoffeeMate/irrxml/*.c) \
            $(wildcard ./CoffeeMate/lua/*.c) \
            $(wildcard ./CoffeeMate/sqlite/*.c) \

SOLIBS    =

ZLIBOBJ   = $(addprefix ./CoffeeMate/zlib/, $(addsuffix .o, $(notdir $(basename $(wildcard ./CoffeeMate/zlib/*.c)))))
LPNGOBJ   = $(addprefix ./CoffeeMate/libpng/, $(addsuffix .o, $(notdir $(basename $(wildcard ./CoffeeMate/libpng/*.c)))))
IXMLOBJ   = $(addprefix ./CoffeeMate/irrxml/, $(addsuffix .o, $(notdir $(basename $(wildcard ./CoffeeMate/irrxml/*.cpp)))))
LLUAOBJ   = $(addprefix ./CoffeeMate/lua/, $(addsuffix .o, $(notdir $(basename $(wildcard ./CoffeeMate/lua/*.c)))))
SQLTOBJ   = $(addprefix ./CoffeeMate/sqlite/, $(addsuffix .o, $(notdir $(basename $(wildcard ./CoffeeMate/sqlite/*.c)))))

ifdef NODEP
LINKOBJS  = AppMain.o CoffeeFile.o CoffeeMemory.o CoffeeZip.o CoffeeTexture.o SugarTga.o SugarRaw.o \
            $(ZLIBOBJ)
else
LINKOBJS  = AppMain.o CoffeeFile.o CoffeeMemory.o CoffeeZip.o CoffeeTexture.o SugarTga.o SugarRaw.o \
            SugarPng.o $(ZLIBOBJ) $(LPNGOBJ) $(LLUAOBJ) $(SQLOBJ)
endif

%.o : %.c
	$(CC) $(CFLAGS) $(INCPATH) -c -o $@ $<

%.o : %.cpp
	$(CXX) $(CXXFLAGS) $(INCPATH) -c -o $@ $<

default : $(TARGET)

$(TARGET) : $(LINKOBJS)
	$(CC) -o ./Build/$@ $(LINKOBJS) $(LIBPATH) $(LDLIBS) $(SOLIBS)

depend :
	$(CC) -MM $(SRCS) $(INCPATH) > make_list

clean :
	$(RM) $(LINKOBJS) $(TARGET)

rebuild : clean all
rebuild_all : clean depend all

.PHONY : clean

